package top.loonglove.common.util.http;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiParam;

import java.io.Serializable;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2021/6/16 0:12
 */
@ApiModel("统一资源响应结果")
public final class HttpResponse implements Serializable {
    private final long serialVersionUID = -0x1312DD5;
    @ApiParam(name = "状态码")
    private int code;
    @ApiParam(name = "接口消息")
    private String message;
    @ApiParam(name = "接口返回数据")
    private Object data;
    @ApiParam(name = "接口返回状态")
    private HttpStatus status;
    @ApiParam(name = "请求类型")
    private HttpMethod requestMethod;
    @ApiParam(name = "响应方法")
    private String responseMethod;
    @ApiParam(name = "错误消息")
    private String errorMessage;
    @ApiParam(name = "错误详情")
    private Throwable error;
    @ApiParam(name = "token")
    private String token;
    @ApiParam(name = "刷新token")
    private String refreshToken;
    public HttpResponse() {
    }

    public HttpResponse(int code, String message, Object data) {
        this.code = code;
        autoCodeOrStatus();
        this.message = message;
        this.data = data;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
        autoCodeOrStatus();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public HttpStatus getStatus() {
        return status;
    }

    public void setStatus(HttpStatus status) {
        this.status = status;
        autoCodeOrStatus();
    }

    public HttpMethod getRequestMethod() {
        return requestMethod;
    }

    public void setRequestMethod(HttpMethod requestMethod) {
        this.requestMethod = requestMethod;
    }

    public String getResponseMethod() {
        return responseMethod;
    }

    public void setResponseMethod(String responseMethod) {
        this.responseMethod = responseMethod;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Throwable getError() {
        return error;
    }

    public void setError(Throwable error) {
        this.error = error;
    }

    private void autoCodeOrStatus(){
        if (status!=null){
            code = status.getCode();
        }else {
            for (HttpStatus httpStatus:HttpStatus.values()){
                if (code==httpStatus.getCode()){
                    status=httpStatus;
                    return;
                }
            }
            status = HttpStatus.DEFAULT_STATUS;
        }
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }
}
